﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Util;
using BLL;
using Model;
public partial class bannerEdit : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Util.UtilWeb.CheckRights(this);
            string strId = Request.QueryString["id"];
            LoadView();
            if (!string.IsNullOrWhiteSpace(strId))
                LoadView(int.Parse(strId));
        }
    }

    private void LoadView()
    {
        var lbl = Master.FindControl("lblTitle") as Label;
        lbl.Text = "幻灯片";
    }

    private void LoadView(int id)
    {
        hdId.Value = id.ToString();
        btnAdd.Text = "修改";
        BllImage bll = new BllImage();
        var obj = bll.GetSingle(id);

        txtName.Text = obj.name;
        txtLink.Text = obj.link;
        hdSmall.Value = obj.src;
        imgSmall.Src = "../" + hdSmall.Value;
        imgSmall.Visible = true;
        hdId.Value = obj.id.ToString();
        this.ddlSort.SelectedValue = obj.sort.ToString();
    }

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        string cmd = btnAdd.Text.Trim();
        BllImage bll = new BllImage();
        string name = txtName.Text.Trim();
        string link = txtLink.Text;
        string sort = this.ddlSort.SelectedValue;
        if (name == "" || link == "" || hdSmall.Value == "")
        {
            Util.UtilWeb.Message(this, Util.KoInfo.FillBlank, null);
            return;

        }

        if (cmd == "添加")
        {
            tb_image obj = new tb_image();
            obj.name = name;
            obj.src = hdSmall.Value;
            obj.link = link;
            obj.sort = int.Parse(sort);
            obj.type = (int)TypeImage.幻灯片;
            if (bll.Add(obj))
            {
                Response.Redirect("BannerList.aspx");
            }
            else
            {
                Util.UtilWeb.Message(this, Util.KoInfo.OperateFailure, null);
            }
        }
        if (cmd == "修改")
        {
            int id = int.Parse(hdId.Value);
            tb_image obj = bll.GetSingle(id);
            obj.name = name;
            obj.src = hdSmall.Value;
            obj.link = link;
            obj.sort = int.Parse(sort);
            obj.type = (int)TypeImage.幻灯片;
            if (bll.Update(obj))
            {
                Response.Redirect("BannerList.aspx");
            }
            else
            {
                Util.UtilWeb.Message(this, Util.KoInfo.OperateFailure, null);
            }
        }
    }

    protected void btnUpload1_Click(object sender, EventArgs e)
    {
        string path = Util.UtilWeb.UploadFile(this, fu, null, "../img/banner/");
        hdSmall.Value = path.Replace("../", "");
        imgSmall.Src = "../" + hdSmall.Value;
        imgSmall.Visible = true;
    }
}